//
//  UGTextField.m
//  LifeBox
//
//  Created by admin on 2019/12/10.
//  Copyright © 2019 Lucky. All rights reserved.
//

#import "UGTextField.h"

@implementation UGTextField

-(instancetype)init{
    if (self == [super init]) {
        self.isCanSpace = YES;
        self.offset = 10;
        self.maxValue = MAXFLOAT;
        UG_WEAKSELF
        [self setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *textField, NSRange range, NSString *string) {
            //得到输入框的内容
            NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
            //判断输入是否有空格
            if ([toBeString spaceJudgment] && !weakSelf.isCanSpace) {
                return NO;
            }
            //如果首字符是空格删除
            if (range.location==0 && [toBeString isEqualToString:@" "]) {
                return NO;
            }
            
            
            if (![string isEqualToString:@""]) {
                if (weakSelf.inputType == InputTypeNumber) {
                    if (![string isPureInt]) {
                        return NO;
                    }
                    if (toBeString.intValue > weakSelf.maxValue) {
                        return NO;
                    }
                    
                }
                if (weakSelf.inputType == InputTypeText && ![string isNormalText]) {
                    return NO;
                }
                if (weakSelf.inputType == InputTypeFloat) {
                    NSArray *temarr = [toBeString componentsSeparatedByString:@"."];
                    if (temarr.count>2) {
                        return NO;
                    }
                    for (NSString *str in temarr) {
                        if (![str isPureInt] && ![str isEqualToString:@""]) {
                            return NO;
                        }
                    }
                    if(weakSelf.floatCount>0 && temarr.count==2){
                        NSString *floatent = temarr.lastObject;
                        if (floatent.length>weakSelf.floatCount) {
                            return NO;
                        }
                    }
                    if (toBeString.floatValue > weakSelf.maxValue) {
                        return NO;
                    }
                }
                
                if (weakSelf.inputType == InputTypeNumberASCII && ![string isNumberASCII]) {
                    return NO;
                }
                
                if ( [string isEqualToString:@" "] && self.textAlignment == NSTextAlignmentRight) {
                    if (range.location == textField.text.length && range.location<self.ug_maximumLimit) {
                        textField.text = [textField.text stringByAppendingString:@"\u00a0"];
                        return NO;
                    }
                    
                }
                
            }
            return YES;
        }];
        [self bk_addEventHandler:^(id sender) {
            self.text = self.text;
        } forControlEvents:UIControlEventEditingChanged|UIControlEventEditingDidBegin];
        
    }
    return self;
}

-(void)setOffset:(CGFloat)offset{
    _offset = offset;
    [self setNeedsLayout];
}

-(void)setInputType:(UGTextFieldInputType)inputType{
    _inputType = inputType;
    //    Defual       = 0, // 1XX
    //    Number       = 1, // %d
    //    Text         = 2, // 不包括表情
    //    Float        = 3, // %.2f
    switch (_inputType) {
            
        case InputTypeNumber:
            self.keyboardType =  UIKeyboardTypeNumberPad;
            break;
        case InputTypeFloat:
            self.keyboardType = UIKeyboardTypeDecimalPad;
            break;
            
        default:
            break;
    }
}
// 未输入时文本的位置，向右缩进10
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, _offset, 0);
}
// 输入后文本的位置，向右缩进10
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, _offset, 0);
}



@end
